﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;


namespace CowboyAndZombies
{
    public class Zumbi : Microsoft.Xna.Framework.DrawableGameComponent
    {
        public Vector2 posicao;
        private Rectangle retangulo;
        public Texture2D textura;
        private int velocidade = 5;
        public float angulo;
        public int vida;
        protected int Yspeed;
        protected int Xspeed;
        protected Random random;

        ////Largura e altura do sprite na textura
        //protected const int METEORWIDTH = 45;
        //protected const int METEORHEIGHT = 45;


        public Zumbi(Game game, ref Texture2D textura)
            : base(game)
        {
            this.textura = textura;
            posicao = new Vector2();

            //Cria o retângulo fonte
            //Isso representa onde a figura so sprite está na superfície
            retangulo = new Rectangle(5, 2, textura.Width, textura.Height);

            //Inicializa o gerador de números randômicos e coloca o meteoro na sua posição inicial
            random = new Random(this.GetHashCode());
            PosicaoInicial();
        }

        protected void PosicaoInicial()
        {
            if (random.Next(Game.Window.ClientBounds.Width - textura.Width) > 300)
            {
                posicao.X = random.Next(Game.Window.ClientBounds.Width - textura.Width);
                posicao.Y = 0;
            }
            else
            {
                posicao.X = 0;
                posicao.Y = random.Next(Game.Window.ClientBounds.Height - textura.Height);
            }

            Yspeed = 1 + random.Next(9);
            Xspeed = random.Next(3) - 1;
        }

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch sBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));

            sBatch.Draw(textura, posicao, retangulo, Color.White);

            base.Draw(gameTime);
        }

        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {

            //Verifica se o meteoro ainda está visível
            if ((posicao.Y >= Game.Window.ClientBounds.Height) ||
                (posicao.X >= Game.Window.ClientBounds.Width) || (posicao.X <= 0))
            {
                PosicaoInicial();
            }

            //Move o meteoro
            posicao.Y += Yspeed;
            posicao.X += Xspeed;

            base.Update(gameTime);
        }

        public bool CheckCollision(Rectangle rect)
        {
            Rectangle spriterect = new Rectangle((int)posicao.X, (int)posicao.Y, textura.Width, textura.Height);

            return spriterect.Intersects(rect);
        }

    }
}
